import { defineStore } from "pinia";
import { LoginPayload } from "../api/index";

const useUserStore = defineStore("user", {
  state: (): LoginPayload => {
    return {
      token: JSON.parse(localStorage.getItem("user") || "{}")?.token,
      email: "",
      username: "",
      channelDescription: "",
      avatar: "",
    };
  },
  actions: {
    setToken(token: string) {
      this.token = token;
    },
    setUserInfo(userInfo: LoginPayload) {
      this.setToken(userInfo.token);
      this.email = userInfo.email;
      this.username = userInfo.username;
      this.channelDescription = userInfo.channelDescription;
      this.avatar = userInfo.avatar;
      console.log("🚀 ~ setUserInfo ~ userInfo:", userInfo);
      localStorage.setItem("user", JSON.stringify(userInfo));
    },
  },
});

export default useUserStore;
